/*
 ** Init_map.c for Init_map in /home/dieng_e/zappy-2011/client_graphique/SDl
 **
 ** Made by el-hadji-malick-ndour dieng
 ** Login   <dieng_e@epitech.net>
 **
 ** Started on  Tue Jun 28 15:49:38 2011 el-hadji-malick-ndour dieng
 ** Last update Thu Jul  7 21:31:06 2011 el-hadji-malick-ndour dieng
 */

#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_video.h>
#include <SDL/SDL_events.h>
#include "header.h"

int Init_Video(params *var)
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        printf("Can't init SDL:  %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }
    atexit(SDL_Quit);
    var->screen = SDL_SetVideoMode(var->SCREEN_WIDTH, var->SCREEN_HEIGHT,
                                   var->SCREEN_BPP, SDL_HWSURFACE);

    if (var->screen == NULL)
    {
        printf("Can't set video mode: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    SDL_FillRect(var->screen, &var->screen->clip_rect, SDL_MapRGB(var->screen->format, 0xFF, 0xFF, 0xFF));


    if (TTF_Init() == -1)
    {
        return 0;
    }
    return EXIT_SUCCESS;
}

int draw_map(params *var)
{
    SDL_WM_SetCaption("Client Graphique Zappy", NULL);
    /*
        var->tmp = Load_Map("./SDL_Img/backgr.jpg");
     */
/*
    var->message = Load_Map("./pierres/mendiane.png");
*/
    /*
        Draw_ttf(var);
     */
    /*
        Make_transparency(var);
     */
    /*
        Blit_Surface(0, 0, var->tmp, var->screen, NULL);
     */
    
/*
        Blit_Surface(0, 0, var->message, var->screen, NULL);
*/
        Blit_Surface(0, 0, Load_Map("./pierres/mendiane.png"), var->screen, NULL);
        Blit_Surface(34, 0, Load_Map("./pierres/linemate.png"), var->screen, NULL);
        Blit_Surface(68, 0, Load_Map("./pierres/phiras.png"), var->screen, NULL);
        Blit_Surface(0, 34, Load_Map("./pierres/deraumere.png"), var->screen, NULL);
        Blit_Surface(0, 68, Load_Map("./pierres/sibur.png"), var->screen, NULL);
        Blit_Surface(102, 0, Load_Map("./pierres/thystame.png"), var->screen, NULL);
        Blit_Surface(0, 102, Load_Map("./pierres/food.png"), var->screen, NULL);
     
    return EXIT_SUCCESS;
}

SDL_Surface *Load_Map(char *str)
{
    SDL_Surface *loadedMap;
    SDL_Surface *OptimizedMap;

    loadedMap = NULL;
    OptimizedMap = NULL;

    loadedMap = IMG_Load(str);

    if (loadedMap != NULL)
    {
        OptimizedMap = SDL_DisplayFormat(loadedMap);
        SDL_FreeSurface(loadedMap);
    }
    return OptimizedMap;
}

void Blit_Surface(int x, int y, SDL_Surface *src, SDL_Surface *dest,
                  SDL_Rect * clip __attribute__((unused)))
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(src, clip, dest, &offset);
}

void Loop_map(params *var)
{
    int quit;

    quit = 1;

    while (quit == 1)
    {
        while (SDL_PollEvent(&var->event))
        {
            if (var->event.type == SDL_QUIT)
            {
                quit = 0;
            }
        }
    }
}